importPackage(javax.servlet.http);
var get = function(req, res) {
	var clear = req.getParameter("clear");
	if (clear) {
		var cookie = new Cookie("foo", 1234);
		cookie.setMaxAge(0);
		res.addCookie(cookie);
		with (res.writer) {
			write("<h1> Cookie 'foo' deleted</h1>");
		}
	} else {
		var cookies = req.getCookies();
		var hasFoo = false;
		if(cookies){
			for(var i in cookies){
				res.writer.write("<h1>"+cookies[i].name+" : ");
				res.writer.write(cookies[i].value+"</h1>");
				if(cookies[i].name=='foo')
					hasFoo = true;
			}
		}
		if(!hasFoo){
			var cookie = new Cookie("foo", 1234);
			res.addCookie(cookie);
			with (res.writer) {
				write("<h1> Cookie 'foo' created</h1>");
			}
		}
	}
	with (res.writer) {
		write("<a href='save-cookie.js?clear=yes'>Clear foo</a>");
		flush();
		close();
	}
};